package com.doradance.main.demo;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.hpplay.bean.CastDeviceInfo;

import java.util.ArrayList;
import java.util.List;

/**
 *  @hide
 *
 */
public class DeviceAdapter extends BaseAdapter {

	private List<CastDeviceInfo> mListDevice = new ArrayList<CastDeviceInfo>();
	private int mSelect = -1;
	private Activity mActivity;
	private int mState = -1;

	public DeviceAdapter(Activity activity) {
		mActivity = activity;
		mSelect = -1;
	}

	 public void setListNotify(List<CastDeviceInfo> listdevice, int position) {
		 try {
			 mSelect = position;
			 mListDevice.clear();
			 mListDevice.addAll(listdevice);
			 notifyDataSetChanged();
		 }catch (Exception e){
			 e.printStackTrace();
		 }
	}

	public void setNotifyConnectState(int state){
		try {
			mState = state;
			notifyDataSetChanged();
		}catch (Exception e){
			e.printStackTrace();
		}
	}

	public void setSelectItem(int select) {
		try {
			mState = -1;
			mSelect = select;
			notifyDataSetChanged();
		}catch (Exception e){
			e.printStackTrace();
		}
	}

	@Override
	public int getCount() {
		return mListDevice.size();
	}

	@Override
	public Object getItem(int position) {
		return mListDevice.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHodler viewHodler;
		try {
			if (convertView == null) {
				convertView = View.inflate(mActivity, com.happly.link.R.layout.hpplay_list_item_layout, null);
				viewHodler = new ViewHodler();
				viewHodler.tv = (TextView) convertView.findViewById(com.happly.link.R.id.item_textview);
				viewHodler.selectImg = (ProgressBar) convertView.findViewById(com.happly.link.R.id.item_progresbar);
				viewHodler.img = (ImageView) convertView.findViewById(com.happly.link.R.id.item_imageview);

				convertView.setTag(viewHodler);
			} else {
				viewHodler = (ViewHodler) convertView.getTag();
			}

			viewHodler.tv.setText(mListDevice.get(position).getHpplayLinkName());


			if (mSelect == position) {
				if(mState == 0){
					viewHodler.img.setSelected(false);
					viewHodler.selectImg.setVisibility(View.GONE);
					viewHodler.img.setVisibility(View.VISIBLE);
				}else if(mState == 1){
					viewHodler.img.setSelected(true);
					viewHodler.selectImg.setVisibility(View.GONE);
					viewHodler.img.setVisibility(View.VISIBLE);
				}else if(mState == -1){
					viewHodler.img.setVisibility(View.GONE);
					viewHodler.selectImg.setVisibility(View.VISIBLE);
				}else{
					viewHodler.img.setVisibility(View.GONE);
					viewHodler.selectImg.setVisibility(View.GONE);
				}
			}else {
				viewHodler.img.setVisibility(View.GONE);
				viewHodler.selectImg.setVisibility(View.GONE);
			}

		}catch (Exception e){
			e.printStackTrace();
		}
		return convertView;
	}

	private static class ViewHodler {
		TextView tv;
		ProgressBar selectImg;
		ImageView img;
	}
}

